const timeoutWrapper = (p, timeout) => Promise.race([p, new Promise((resolve, reject) => {
    setTimeout(() => {
        reject(`${timeout}ms timeout`)
    }, timeout)
})])


const abortWrapper = (p, cancel) => Promise.race([p, new Promise((resolve, reject) => {
        cancel(reject)
    })])


const request = () => {
    return new Promise(resolve => {
        setTimeout(()=>{
            resolve('收到服务端数据')
        }, 1000)
    })
}

timeoutWrapper(request(), 500).then((a)=>{
    console.log('success', a)
}).catch((e)=>{
    console.log('error', e);
})

timeoutWrapper(request(), 1500).then((a)=>{
    console.log('success', a)
}).catch((e)=>{
    console.log('error', e);
})

abortWrapper(request(), (cancel)=>{
    setTimeout(()=>{
        cancel('用户手动停止')
    }, 500)
}).then(res => {
    console.log('success', res)
}).catch(e => {
    console.log('error', e)
})

